package com.whfc.wxmp.api.fse.service.impl;

import com.whfc.common.enums.BindFlag;
import com.whfc.common.result.ListData;
import com.whfc.fse.dto.FseCrawlerDTO;
import com.whfc.fse.dto.FseCrawlerDataDTO;
import com.whfc.fse.dto.FseCrawlerDataLogDTO;
import com.whfc.fse.dto.FseFvsDeviceDTO;
import com.whfc.fse.service.FseCrawlerService;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.wxmp.api.fse.service.WxFseCrawlerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/11/4 16:36
 */
@Service
public class WxFseCrawlerServiceImpl implements WxFseCrawlerService {

    @DubboReference(interfaceClass = FseCrawlerService.class, version = "1.0.0")
    private FseCrawlerService fseCrawlerService;
    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @Override
    public List<FseCrawlerDTO> list(Integer deptId, Integer bindFlag, Integer netState) {
        List<FseCrawlerDTO> list = fseCrawlerService.list(deptId, BindFlag.BIND.getValue(), null).getList();
        list.forEach(fseCrawlerDTO -> {
            List<FseFvsDeviceDTO> fseFvsDeviceDTOS = fvsList(fseCrawlerDTO.getCrawlerId());
            fseCrawlerDTO.setFvsList(fseFvsDeviceDTOS);
        });
        return list;

    }

    @Override
    public FseCrawlerDataDTO getLatest(Integer crawlerId) {
        return fseCrawlerService.getLatest(crawlerId);
    }

    @Override
    public List<FseCrawlerDataLogDTO> getCrawlerLog(Integer crawlerId, Date date) {
        return fseCrawlerService.getCrawlerLog(crawlerId, date);
    }

    @Override
    public List<FseCrawlerDataLogDTO> getCrawlerLog(Integer crawlerId, Date date, String field) {
        return fseCrawlerService.getCrawlerLog(crawlerId, date, field);
    }

    @Override
    public List<FseFvsDeviceDTO> fvsList(Integer crawlerId) {
        List<FseFvsDeviceDTO> list = fseCrawlerService.getFvsDeviceList(crawlerId);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FseFvsDeviceDTO> map = new HashMap<>();
        for (FseFvsDeviceDTO fvsDeviceDTO : list) {
            map.put(fvsDeviceDTO.getFvsDeviceId(), fvsDeviceDTO);
        }
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FseFvsDeviceDTO fseFvsDeviceDTO = map.get(fvsDeviceDTO.getId());
            if (fseFvsDeviceDTO != null) {
                fseFvsDeviceDTO.setFvsDevice(fvsDeviceDTO);
            }
        }
        return new ArrayList<>(map.values());
    }
}
